﻿using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.MES.Customer.Infrastructure.Repository
{
    /// <summary>
    /// 支出仓储实现层
    /// </summary>
    public class AdditionalExpenditureRepository : BaseRepository<AdditionalExpenditure>, IAdditionalExpenditureRepository
    {
        private readonly ISqlSugarClient _db;
        public AdditionalExpenditureRepository(ISqlSugarClient db) : base(db)
        {
            _db = db;
        }
        /// <summary>
        /// 支出添加
        /// </summary>
        /// <param name="additionalExpenditure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int AddadditionalExpenditure(AdditionalExpenditure additionalExpenditure)
        {
            var result=_db.Insertable(additionalExpenditure).ExecuteCommand();
            return result;
        }
        /// <summary>
        /// 支出删除
        /// </summary>
        /// <param name="NewExpenditureId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<AdditionalExpenditure> DeleteAdditionalExpenditure(int NewExpenditureId)
        {
            APIResult<AdditionalExpenditure> apiResult = new APIResult<AdditionalExpenditure>();
             _db.Deleteable<AdditionalExpenditure>(NewExpenditureId).ExecuteCommand();
            apiResult.SuccessDel();
            return apiResult;
        }
        /// <summary>
        /// 批量删除支出
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<List<AdditionalExpenditure>> DeleteAdditionalExpenditure(List<AdditionalExpenditure> list)
        {
            APIResult<List<AdditionalExpenditure>> apiResult = new APIResult<List<AdditionalExpenditure>>();
            foreach (var item in list)
            {
                item.IsDeleted=true;   
            }
            _db.Updateable(list).ExecuteCommand();  
            apiResult.SuccessDel();
            return apiResult;

        }

        /// <summary>
        /// 反填支出信息
        /// </summary>
        /// <param name="NewExpenditureId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public AdditionalExpenditure EditAdditionalExpenditure(int NewExpenditureId)
        {
            var result = _db.Queryable<AdditionalExpenditure>().InSingle(NewExpenditureId);
            return result;
        }

        /// <summary>
        /// 支出显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public PageApiResut<AdditionalExpenditureDTO> GetAdditionalExpenditure(int PageIndex, int PageSize)
        {
                PageApiResut<AdditionalExpenditureDTO> pageApiResut = new PageApiResut<AdditionalExpenditureDTO>();
            int totalCount = _db.Queryable<AdditionalExpenditure>().Where(x => x.IsDeleted == false).Count();

            var query = _db.Queryable<AdditionalExpenditure, ExpenditureType>((A, B) => new JoinQueryInfos(
                JoinType.Inner, A.ExpenditureTypeId == B.ExpenditureTypeId
                )).Select((A, B) => new AdditionalExpenditureDTO
                {
                    ExpenditureTypeId = B.ExpenditureTypeId,
                    AccountName = A.AccountName,
                    AggregateAmount = A.AggregateAmount,
                    AmountPaid = A.AmountPaid,
                    Attachment = A.Attachment,
                    BankAccount = A.BankAccount,
                    DepositBank = A.DepositBank,
                    ExpenditureTypeName = B.ExpenditureTypeName,
                    NetworkName = A.NetworkName,
                    NewExpenditureId = A.NewExpenditureId,
                    Payee = A.Payee,
                    PaymentAmount = A.PaymentAmount,
                    PurchaseDate = A.PurchaseDate,
                    PurchaseOrderSubject = A.PurchaseOrderSubject,
                    Remark = A.Remark,
                    StorageStatus = A.StorageStatus,
                    SupportDate = A.SupportDate,
                    SupportingTopic = A.SupportingTopic,
                    TheInvoiceNumber = A.TheInvoiceNumber
                }).OrderByDescending(A => A.NewExpenditureId).ToPageList(PageIndex, PageSize);
                
                pageApiResut.TotalCount = totalCount;
                //query = query.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
                pageApiResut.Data = query.ToList(); // 根据需排序字段进行排序
            ; // 分页查询;
            return pageApiResut;
            }
        /// <summary>
        /// 获取字符类型
        /// </summary>
        /// <returns></returns>
        public List<ExpenditureType> GetExpenditureType()
        {
            var result=_db.Queryable<ExpenditureType>().ToList();
            return result;
        }
        /// <summary>
        /// 更新支出信息
        /// </summary>
        /// <param name="additionalExpenditure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<AdditionalExpenditure> UpdateAdditionalExpenditure(AdditionalExpenditure additionalExpenditure)
        {
            APIResult<AdditionalExpenditure> apiResult = new APIResult<AdditionalExpenditure>();
            _db.Updateable(additionalExpenditure).ExecuteCommand();
            apiResult.SuccessUpdate();
            return apiResult;

        }
    }
}
